using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Diagnostics.Contracts;
using System.Windows;
using Konst3d.Efficiency.Suite;

namespace PEM
{
	internal partial class MilestonesViewModel
	{
		#region Commands

		public abstract class BaseCommand : ICommand
		{
			#region Constructor

			public BaseCommand(MilestonesViewModel model)
			{
				Contract.Requires(model != null);
				_model = model;
			}

			#endregion // Constructor

			#region ICommand Implementation

			public event System.EventHandler CanExecuteChanged;

			public abstract bool CanExecute(object parameter);
			public abstract void Execute(object parameter);

			protected void NotifyCanExecuteChanged(EventArgs args = null)
			{
				if (CanExecuteChanged != null)
				{
					CanExecuteChanged(this, args ?? EventArgs.Empty);
				}
			}

			#endregion

			#region Fields

			protected MilestonesViewModel _model;

			#endregion // Fields
		}

		public class AddMilestoneCommand : BaseCommand
		{
			#region Constructor

			public AddMilestoneCommand(MilestonesViewModel model):
				base(model)
			{
				_model.SuiteOpened += OnSuiteChange;
				_model.SuiteClosed += OnSuiteChange;
				_model.SuiteCreated += OnSuiteChange;
				_model.SuiteDeleted += OnSuiteChange;
			}

			#endregion // Constructor

			#region ICommand Members

			public override bool CanExecute(object parameter)
			{
				return _model.Suite != null;
			}

			public override void Execute(object parameter)
			{
				_model.AddMilestone();
			}

			#endregion

			#region Event Handlers

			private void OnSuiteChange(ViewModelBase sender)
			{
				NotifyCanExecuteChanged();
			}

			#endregion Event Handlers
		}

		public class RemoveMilestoneCommand : BaseCommand
		{
			#region Constructor

			public RemoveMilestoneCommand(MilestonesViewModel model) :
				base(model)
			{
				_model.MilestoneSelected += OnMilestoneSelected;
			}

			#endregion // Constructor

			#region ICommand Members

			public override bool CanExecute(object parameter)
			{
				return _model.Selected != null;
			}

			public override void Execute(object parameter)
			{
				_model.RemoveMilestone();
			}

			#endregion

			#region Event Handlers

			private void OnMilestoneSelected(ViewModelBase sender)
			{
				NotifyCanExecuteChanged();
			}

			#endregion Event Handlers
		}

		public class EditMilestoneCommand : BaseCommand
		{
			#region Constructor

			public EditMilestoneCommand(MilestonesViewModel model) :
				base(model)
			{
				_model.MilestoneSelected += OnMilestoneSelected;
			}

			#endregion // Constructor

			#region ICommand Members

			public override bool CanExecute(object parameter)
			{
				return _model.Selected != null;
			}

			public override void Execute(object parameter)
			{
				_model.EditMilestone();
			}

			#endregion

			#region Event Handlers

			private void OnMilestoneSelected(ViewModelBase sender)
			{
				NotifyCanExecuteChanged();
			}

			#endregion Event Handlers
		}

		#endregion // Commands

		#region Commands Collection

		public class CommandsCollection
		{
			#region Properties

			public AddMilestoneCommand AddMilestone
			{
				get;
				private set;
			}

			public RemoveMilestoneCommand RemoveMilestone
			{
				get;
				private set;
			}

			public EditMilestoneCommand EditMilestone
			{
				get;
				private set;
			}

			#endregion // Properties

			#region Constructor

			public CommandsCollection(MilestonesViewModel host)
			{
				Contract.Requires(host != null);

				AddMilestone = new AddMilestoneCommand(host);
				RemoveMilestone = new RemoveMilestoneCommand(host);
				EditMilestone = new EditMilestoneCommand(host);
			}

			#endregion // Constructor
		}

		#endregion // Commands Collection

		public void AddMilestone()
		{
			Contract.Requires(Suite != null);

			if (_createEditMilestoneDlg.ShowDialog(_parentWindow))
			{
				Suite.Milestones.Add(_createEditMilestoneDlg.MilestoneDate,
					_createEditMilestoneDlg.MilestoneTitle,
					_createEditMilestoneDlg.MilestoneDescription);

				UpdateMilestonesList();
			}
		}

		public void RemoveMilestone()
		{
			Contract.Requires(Suite != null);
			Contract.Requires(Selected != null);

			if (MessageBox.Show(_parentWindow, "Are you sure, you wish to delete this milestone?", "Delete milestone?", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
			{
				Suite.Milestones.Remove(Selected.ID);
				UpdateMilestonesList();
			}
		}

		public void EditMilestone()
		{
			Contract.Requires(Suite != null);
			Contract.Requires(Selected != null);

			if (_createEditMilestoneDlg.ShowDialog(_parentWindow, Selected))
			{
				Selected.Date = _createEditMilestoneDlg.MilestoneDate;
				Selected.Title = _createEditMilestoneDlg.MilestoneTitle;
				Selected.Description = _createEditMilestoneDlg.MilestoneDescription;

				UpdateMilestonesList(); // Milestones.SignalItemChanged(Selected); ... sorting ???
			}
		}
	}
}
